package com.xjzd.std.clashloader;

public class ClassLoaderExample {
    public static void main(String[] args) {
        // 获取各个类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        ClassLoader platformClassLoader = systemClassLoader.getParent();
        ClassLoader bootstrapClassLoader = platformClassLoader.getParent(); // 返回null

        System.out.println("系统类加载器: " + systemClassLoader);
        System.out.println("平台类加载器: " + platformClassLoader);
        System.out.println("启动类加载器: " + bootstrapClassLoader);

        // 演示自定义类加载器可以创建多个
        CustomClassLoader loader1 = new CustomClassLoader();
        CustomClassLoader loader2 = new CustomClassLoader();
        System.out.println("自定义加载器1: " + loader1);
        System.out.println("自定义加载器2: " + loader2);
        System.out.println("是否相同: " + (loader1 == loader2));
    }
}

// 自定义类加载器
class CustomClassLoader extends ClassLoader {
    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        // 自定义加载逻辑
        byte[] classData = loadClassData(name);
        return defineClass(name, classData, 0, classData.length);
    }

    private byte[] loadClassData(String className) {
        // 实现加载类数据的逻辑
        return new byte[0];
    }
}